<?php
/**
 * 商品属性值
 * Author: hades
 * DateTime: 2020/5/16 16:17
 */

namespace App\Controller\admin;

use App\Model\GProperty;
use App\Model\GPropertyValue;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * Class CategoryController
 * @package App\Controller\admin
 * @Controller(prefix="a/property/value")
 * @Middleware(\App\Middleware\AdminMiddleware::class)
 */
class PropertyValueController extends AdminBase {

    /**
     * @RequestMapping(path="lists",methods="get")
     * @return array
     */
    public function getLists() {
        $pid = (int) $this->get('pid');
        $lists = GPropertyValue::query()
            ->where('pid', $pid)
            ->select('id', 'value')
            ->get();
        return success(['lists' => $lists]);
    }

    /**
     * 添加、编辑
     * @RequestMapping(path="save",methods="post")
     * @return array
     */
    public function save() {
        $value = $this->body('value');
        $pid = $this->body('pid');
        $id = $this->body('id');
        if (empty($name) || empty($pid)) {
            return error('参数错误');
        }
        $value = mb_substr($value, 0, 20);
        // 查询父级
        $cid = GProperty::query()->where('id', $pid)->value('cid');
        if ($cid === null) {
            return error('属性不存在');
        }
        $data = ['value' => $value, 'cid' => $cid, 'pid' => $pid];
        if ($id) {
            $r = GPropertyValue::query()->where('id', $id)->update($data);
        } else {
            $r = GPropertyValue::query()->insert($data);
        }
        return $r ? success() : error();
    }
}
